package com.xb.controller;

import com.xb.data.Result;
import com.xb.page.PageBean;
import com.xb.service.SearchService;
import com.xb.vo.CourseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@Api
@ApiOperation(value = "课程搜索功能")
@RequestMapping("search")
public class SearchController {


    @Autowired
    private SearchService searchService;

    @GetMapping("searchCourcesByName")
    @ApiOperation(value = "es根据课程名称条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseName", value = "课程名称关键字", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageNum", value = "当前页码数", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", required = false, paramType = "query")
    })
    public Result searchCourcesByName(@RequestParam String courseName, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize) {
        try {
            PageBean<CourseVO> data = searchService.searchCourcesByName(courseName, pageNum, pageSize);
            return Result.OK().setData(data);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @GetMapping("searchCourcesByLabel")
    @ApiOperation(value = "es根据标签条件查询")
    @ApiImplicitParam(name = "label", value = "课程标签关键字", paramType = "query")
    public Result searchCourcesByLabel(@RequestParam String label) {
        try {
            List<CourseVO> list = searchService.searchCourcesByLabel(label);
            return Result.OK().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
